﻿using System;
using System.Collections.Generic;
using System.Linq;
using Z13qu.Code;
using Z13qu.Model;
using Z13qu.Service;

namespace Z13qu.Service
{
    public class Sys_ModuleButtonService : ServiceBase<Sys_ModuleButton>
    {

        public List<Sys_ModuleButton> GetList(string moduleId = "")
        {
            var expression = ExtLinq.True<Sys_ModuleButton>();
            if (!string.IsNullOrEmpty(moduleId))
            {
                expression = expression.And(t => t.ModuleId == moduleId);
            }
            return IQueryable(expression).OrderBy(t => t.SortCode).ToList();
        }
        
        public void DeleteById(string Id)
        {
            if (IQueryable().Count(t => t.ParentId.Equals(Id)) > 0)
            {
                throw new Exception("删除失败！操作的对象包含了下级数据。");
            }
            else
            {
                Delete(t => t.Id == Id);
            }
        }
        public void SubmitForm(Sys_ModuleButton moduleButtonEntity, string keyValue)
        {
            moduleButtonEntity.Id = keyValue;
            if (!string.IsNullOrEmpty(keyValue))
            {
                Update(moduleButtonEntity);
            }
            else
            {
                Insert(moduleButtonEntity);
            }
        }
        public void SubmitCloneButton(string moduleId, string Ids)
        {
            string[] ArrayId = Ids.Split(',');
            var data = this.GetList();
            List<Sys_ModuleButton> entitys = new List<Sys_ModuleButton>();
            foreach (string item in ArrayId)
            {
                Sys_ModuleButton moduleButtonEntity = data.Find(t => t.Id == item);
                moduleButtonEntity.ModuleId = moduleId;
                entitys.Add(moduleButtonEntity);
            }
            Insert(entitys);
        }
        
    }
}
